package cn.dnui_wwt524.dnuifood_16110100524.model;

import cn.dnui_wwt524.dnuifood_16110100524.listener.OnRetrofitListener;
import cn.dnui_wwt524.dnuifood_16110100524.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class BaseModel {
    public Retrofit retrofit;
    private String Base_URL="http://172.24.10.175:8080/foodService/";
    public BaseModel(){
        retrofit = new Retrofit.Builder().baseUrl(Base_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }
    public   <T>void bindCallBack(Call<T> call, final OnRetrofitListener listener, final int flag){
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                if(response.body()==null){
                    listener.onFailure("解析错误");
                }else {
                    listener.onSuccess(response.body(),flag);
                }
            }
            @Override
            public void onFailure(Call<T> call, Throwable t) {
                listener.onFailure("联网异常");

            }
        });
    }

}
